home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / ghost / gs403src_png.lha / gs4.03 / libpng / pngwtran.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  9KB  |  348 lines

  1.  
  2. /* pngwtran.c - transforms the data in a row for png writers
  3.  
  4.    libpng 1.0 beta 3 - version 0.89
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    May 25, 1996
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #include "png.h"
  12.  
  13. /* transform the data according to the users wishes.  The order of
  14.    transformations is significant. */
  15. void
  16. png_do_write_transformations(png_structp png_ptr)
  17. {
  18. #if defined(PNG_WRITE_FILLER_SUPPORTED)
  19.    if (png_ptr->transformations & PNG_FILLER)
  20.       png_do_write_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
  21.          png_ptr->flags);
  22. #endif
  23. #if defined(PNG_WRITE_PACK_SUPPORTED)
  24.    if (png_ptr->transformations & PNG_PACK)
  25.       png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
  26.          png_ptr->bit_depth);
  27. #endif
  28. #if defined(PNG_WRITE_SHIFT_SUPPORTED)
  29.    if (png_ptr->transformations & PNG_SHIFT)
  30.       png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
  31.          &(png_ptr->shift));
  32. #endif
  33. #if defined(PNG_WRITE_SWAP_SUPPORTED)
  34.    if (png_ptr->transformations & PNG_SWAP_BYTES)
  35.       png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
  36. #endif
  37. #if defined(PNG_WRITE_BGR_SUPPORTED)
  38.    if (png_ptr->transformations & PNG_BGR)
  39.       png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
  40. #endif
  41. #if defined(PNG_WRITE_INVERT_SUPPORTED)
  42.    if (png_ptr->transformations & PNG_INVERT_MONO)
  43.       png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
  44. #endif
  45. }
  46.  
  47. #if defined(PNG_WRITE_PACK_SUPPORTED)
  48. /* pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
  49.    row_info bit depth should be 8 (one pixel per byte).  The channels
  50.    should be 1 (this only happens on grayscale and paletted images) */
  51. void
  52. png_do_pack(png_row_infop row_info, png_bytep row, png_byte bit_depth)
  53. {
  54.    if (row_info && row && row_info->bit_depth == 8 &&
  55.       row_info->channels == 1)
  56.    {
  57.       switch (bit_depth)
  58.       {
  59.          case 1:
  60.          {
  61.             png_bytep sp;
  62.             png_bytep dp;
  63.             int mask;
  64.             png_int_32 i;
  65.             int v;
  66.  
  67.             sp = row;
  68.             dp = row;
  69.             mask = 0x80;
  70.             v = 0;
  71.             for (i = 0; i < row_info->width; i++)
  72.             {
  73.                if (*sp)
  74.                   v |= mask;
  75.                sp++;
  76.                if (mask > 1)
  77.                   mask >>= 1;
  78.                else
  79.                {
  80.                   mask = 0x80;
  81.                   *dp = (png_byte)v;
  82.                   dp++;
  83.                   v = 0;
  84.                }
  85.             }
  86.             if (mask != 0x80)
  87.                *dp = (png_byte)v;
  88.             break;
  89.          }
  90.          case 2:
  91.          {
  92.             png_bytep sp;
  93.             png_bytep dp;
  94.             int shift;
  95.             png_int_32 i;
  96.             int v;
  97.             png_byte value;
  98.  
  99.             sp = row;
  100.             dp = row;
  101.             shift = 6;
  102.             v = 0;
  103.             for (i = 0; i < row_info->width; i++)
  104.             {
  105.                value = (png_byte)(*sp & 0x3);
  106.                v |= (value << shift);
  107.                if (shift == 0)
  108.                {
  109.                   shift = 6;
  110.                   *dp = (png_byte)v;
  111.                   dp++;
  112.                   v = 0;
  113.                }
  114.                else
  115.                   shift -= 2;
  116.                sp++;
  117.             }
  118.             if (shift != 6)
  119.                *dp = (png_byte)v;
  120.             break;
  121.          }
  122.          case 4:
  123.          {
  124.             png_bytep sp;
  125.             png_bytep dp;
  126.             int shift;
  127.             png_int_32 i;
  128.             int v;
  129.             png_byte value;
  130.  
  131.             sp = row;
  132.             dp = row;
  133.             shift = 4;
  134.             v = 0;
  135.             for (i = 0; i < row_info->width; i++)
  136.             {
  137.                value = (png_byte)(*sp & 0xf);
  138.                v |= (value << shift);
  139.  
  140.                if (shift == 0)
  141.                {
  142.                   shift = 4;
  143.                   *dp = (png_byte)v;
  144.                   dp++;
  145.                   v = 0;
  146.                }
  147.                else
  148.                   shift -= 4;
  149.  
  150.                sp++;
  151.             }
  152.             if (shift != 4)
  153.                *dp = (png_byte)v;
  154.             break;
  155.          }
  156.       }
  157.       row_info->bit_depth = bit_depth;
  158.       row_info->pixel_depth = (png_uint_16)(bit_depth * row_info->channels);
  159.       row_info->rowbytes =
  160.          ((row_info->width * row_info->pixel_depth + 7) >> 3);
  161.    }
  162. }
  163. #endif
  164.  
  165. #if defined(PNG_WRITE_SHIFT_SUPPORTED)
  166. /* shift pixel values to take advantage of whole range.  Pass the
  167.    true number of bits in bit_depth.  The row should be packed
  168.    according to row_info->bit_depth.  Thus, if you had a row of
  169.    bit depth 4, but the pixels only had values from 0 to 7, you
  170.    would pass 3 as bit_depth, and this routine would translate the
  171.    data to 0 to 15. */
  172. void
  173. png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
  174. {
  175.    if (row && row_info &&
  176.       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  177.    {
  178.       int shift_start[4], shift_dec[4];
  179.       int channels;
  180.  
  181.       channels = 0;
  182.       if (row_info->color_type & PNG_COLOR_MASK_COLOR)
  183.       {
  184.          shift_start[channels] = row_info->bit_depth - bit_depth->red;
  185.          shift_dec[channels] = bit_depth->red;
  186.          channels++;
  187.          shift_start[channels] = row_info->bit_depth - bit_depth->green;
  188.          shift_dec[channels] = bit_depth->green;
  189.          channels++;
  190.          shift_start[channels] = row_info->bit_depth - bit_depth->blue;
  191.          shift_dec[channels] = bit_depth->blue;
  192.          channels++;
  193.       }
  194.       else
  195.       {
  196.          shift_start[channels] = row_info->bit_depth - bit_depth->gray;
  197.          shift_dec[channels] = bit_depth->gray;
  198.          channels++;
  199.       }
  200.       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  201.       {
  202.          shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
  203.          shift_dec[channels] = bit_depth->alpha;
  204.          channels++;
  205.       }
  206.  
  207.       /* with low row dephts, could only be grayscale, so one channel */
  208.       if (row_info->bit_depth < 8)
  209.       {
  210.          png_bytep bp;
  211.          png_uint_32 i;
  212.          int j;
  213.          png_byte mask;
  214.  
  215.          if (bit_depth->gray == 1 && row_info->bit_depth == 2)
  216.             mask = 0x55;
  217.          else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
  218.             mask = 0x11;
  219.          else
  220.             mask = 0xff;
  221.  
  222.          for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
  223.          {
  224.             int v;
  225.  
  226.             v = *bp;
  227.             *bp = 0;
  228.             for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
  229.             {
  230.                if (j > 0)
  231.                   *bp |= (png_byte)((v << j) & 0xff);
  232.                else
  233.                   *bp |= (png_byte)((v >> (-j)) & mask);
  234.             }
  235.          }
  236.       }
  237.       else if (row_info->bit_depth == 8)
  238.       {
  239.          png_bytep bp;
  240.          png_uint_32 i;
  241.          int j;
  242.  
  243.          for (bp = row, i = 0; i < row_info->width; i++)
  244.          {
  245.             int c;
  246.  
  247.             for (c = 0; c < channels; c++, bp++)
  248.             {
  249.                int v;
  250.  
  251.                v = *bp;
  252.                *bp = 0;
  253.                for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  254.                {
  255.                   if (j > 0)
  256.                      *bp |= (png_byte)((v << j) & 0xff);
  257.                   else
  258.                      *bp |= (png_byte)((v >> (-j)) & 0xff);
  259.                }
  260.             }
  261.          }
  262.       }
  263.       else
  264.       {
  265.          png_bytep bp;
  266.          png_uint_32 i;
  267.          int j;
  268.  
  269.          for (bp = row, i = 0;
  270.             i < row_info->width * row_info->channels;
  271.             i++)
  272.          {
  273.             int c;
  274.  
  275.             for (c = 0; c < channels; c++, bp += 2)
  276.             {
  277.                png_uint_16 value, v;
  278.  
  279.                v = (png_uint_16)(((png_uint_16)(*bp) << 8) +
  280.                   (png_uint_16)(*(bp + 1)));
  281.                value = 0;
  282.                for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  283.                {
  284.                   if (j > 0)
  285.                      value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
  286.                   else
  287.                      value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
  288.                }
  289.                *bp = (png_byte)(value >> 8);
  290.                *(bp + 1) = (png_byte)(value & 0xff);
  291.             }
  292.          }
  293.       }
  294.    }
  295. }
  296. #endif
  297.  
  298. #ifdef PNG_WRITE_FILLER_SUPPORTED
  299. /* remove filler byte */
  300. void
  301. png_do_write_filler(png_row_infop row_info, png_bytep row,
  302.    png_byte flags)
  303. {
  304.    if (row && row_info && row_info->color_type == PNG_COLOR_TYPE_RGB &&
  305.       row_info->bit_depth == 8)
  306.    {
  307.       if (flags & PNG_FLAG_FILLER_AFTER)
  308.       {
  309.          png_bytep sp, dp;
  310.  
  311.          png_uint_32 i;
  312.  
  313.          for (i = 1, sp = row + 4, dp = row + 3;
  314.             i < row_info->width;
  315.             i++)
  316.          {
  317.             *dp++ = *sp++;
  318.             *dp++ = *sp++;
  319.             *dp++ = *sp++;
  320.             sp++;
  321.          }
  322.          row_info->channels = 3;
  323.          row_info->pixel_depth = 24;
  324.          row_info->rowbytes = row_info->width * 3;
  325.       }
  326.       else
  327.       {
  328.          png_bytep sp, dp;
  329.          png_uint_32 i;
  330.  
  331.          for (i = 0, sp = row, dp = row;
  332.             i < row_info->width;
  333.             i++)
  334.          {
  335.             sp++;
  336.             *dp++ = *sp++;
  337.             *dp++ = *sp++;
  338.             *dp++ = *sp++;
  339.          }
  340.          row_info->channels = 3;
  341.          row_info->pixel_depth = 24;
  342.          row_info->rowbytes = row_info->width * 3;
  343.       }
  344.    }
  345. }
  346. #endif
  347.  
  348.